package com.qxsx.midi.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.exam.ClassVideoDetailsActivity;
import com.qxsx.midi.activity.exam.ClassVideoListActivity;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Joney on 2015/9/11.
 * <p/>
 * 证书查询
 */
public class CertificateSearchActivity extends BaseActivity implements OnClickListener {
    private CustomProgress customProgress;
    //查询
    private Button btn_search;

    private EditText edt_search;
    String id, midi_id, real_name,
            sex,
            nationality,
            people,
            major_name,
            start_end_grade,
            grade_num,
            certificate_date,
            certificate_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_certificate_search);
        setTitle("证书查询");
        setNavBtn(R.mipmap.back, 0);
        findById();
    }

    private void findById() {

        btn_search = (Button) findViewById(R.id.btn_search);
        btn_search.setOnClickListener(this);

        edt_search = (EditText) findViewById(R.id.edt_search);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }


    private void certificate() {
        customProgress = CustomProgress.show(CertificateSearchActivity.this, "正在查询，请稍后....", true, null);

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();


        try {
            object.put("certificate_num", edt_search.getText().toString().trim());

            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("510", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Log.w("array", array + "");
                if (array.length() == 0) {
                    ShowToast("您输入的证书账号有误");
                    customProgress.dismiss();
                    return;
                } else {
                    try {
                        JSONObject object1 = array.getJSONObject(0);
                        id = object1.getString("id");
                        midi_id = object1.getString("midi_id");
                        real_name = object1.getString("real_name");
                        sex = object1.getString("sex");
                        nationality = object1.getString("nationality");
                        people = object1.getString("people");
                        major_name = object1.getString("major_name");
                        start_end_grade = object1.getString("start_end_grade");
                        grade_num = object1.getString("grade_num");
                        certificate_date = object1.getString("certificate_date");
                        certificate_num = object1.getString("certificate_num");
                        //跳转到证书详情页
                        Intent it = new Intent(CertificateSearchActivity.this, CertificateSearchFinishActivity.class);
                        //it.putExtra("search", edt_search.getText().toString().trim());//传递证书ID
                        it.putExtra("id", id);//传递证书ID
                        it.putExtra("midi_id", midi_id);//传递证书ID
                        it.putExtra("real_name", real_name);//传递证书ID
                        it.putExtra("sex", sex);//传递证书ID
                        it.putExtra("nationality", nationality);//传递证书ID
                        it.putExtra("people", people);//传递证书ID
                        it.putExtra("major_name", major_name);//传递证书ID
                        it.putExtra("start_end_grade", start_end_grade);//传递证书ID
                        it.putExtra("grade_num", grade_num);//传递证书ID
                        it.putExtra("certificate_date", certificate_date);//传递证书ID
                        it.putExtra("certificate_num", certificate_num);//传递证书ID



                        startActivity(it);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                customProgress.dismiss();
            }
        });


    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_search:
                certificate();



                break;
        }
    }
}
